#!/usr/bin/env python
#Filename:bbs_login.py
import sys
import httplib
import urllib
import time
import string

url = 'bbs.whu.edu.cn'
charset='utf-8'

def file_write(filename,strn):
    ff=file(filename,'w')
    ff.write(strn)
    ff.close()

def login(userid,passwd):
    while True:
        data="id=%s&passwd=%s&webtype=wforum&kick_multi=1"%(userid,passwd)
        global url
        path = '/bbslogin.php'
        headers = {"Content-type":"application/x-www-form-urlencoded","Content-length":len(data)}
        h=httplib.HTTPConnection(url)
        h.request('POST',path,data,headers)
        reply=h.getresponse()
        #file_write("tmp.html",reply.read())
        #print reply.status
        if reply.status==302:
            break
        else:
            h.close()            
        
    cookies=""
    key=""
    num=""
    userid=""
    list_ck=reply.getheader('Set-Cookie')
    #print list_ck
    if list_ck.find("WWWPARAMS")==0:
        flag=1
    else:
        flag=0
    list_cks=list_ck.split(";")
    
    #print list_cks,flag

    i=list_cks[flag].find("UTMPKEY")
    #print i
    j=len(list_cks[flag])
    if i>=0:
        key=list_cks[flag][i+8:j]
        #print key
    flag=flag+1
    i=list_cks[flag].find("UTMPNUM")
    #print i
    j=len(list_cks[flag])
    if i>=0:
        num=list_cks[flag][i+8:j]
        #print num
    flag=flag+1
    i=list_cks[flag].find("UTMPUSERID")
    j=len(list_cks[flag])
    if i>=0:
        userid=list_cks[flag][i+11:j]
        #print userid

    cookies="%s,%s,%s"%(key,num,userid)
    h.close()
    return cookies

def logout(cookies):
    global url
    ck=cookies.split(",")
    #print ck
    header="WWWPARAMS=0;UTMPKEY=%s;UTMPNUM=%s;UTMPUSERID=%s"%(ck[0],ck[1],ck[2])
    
    h=httplib.HTTP(url)
    h.putrequest("GET","/bbslogout.php")
    h.putheader("Cookie",header)
    h.endheaders()
    h.getreply()
    h.close()
    print "Logout Successfully!"
    return

def post(cookies,subject,content, board):
    global url
    ck=cookies.split(",")
    header="WWWPARAMS=0;UTMPKEY=%s;UTMPNUM=%s;UTMPUSERID=%s"%(ck[0],ck[1],ck[2])
    
    subject=urllib.quote(unicode(subject, 'utf-8').encode('gbk'))
    content=urllib.quote(unicode(content, 'utf-8').encode('gbk'))
    data="board=%s&reID=0&subject=%s&Content=%s"%(board, subject,content)
    path = '/wForum/dopostarticle.php'
    headers = {"Content-type":"application/x-www-form-urlencoded","Content-length":len(data)}
    headers['Cookie']=header
    h=httplib.HTTPConnection(url)
    h.request('POST',path,data,headers)
    reply=h.getresponse()
    #print reply.getheaders()
    #print "\n"
    #print reply.read()
    h.close()
    #print reply.status
    return reply.status
   
def main():
    httplib.HTTPConnection.debuglevel=1
    
    if len(sys.argv)!=3:
        print "Usage: %s UserName Password"%sys.argv[0]
        sys.exit(0)
    userid=sys.argv[1]
    passwd=sys.argv[2]
#login
    start=time.time()
    
    cookies=login(userid,passwd)
    #print cookies
    #logout(cookies)
    post(cookies,"hello","I was there!",  'Test')
    
    #time.sleep(1)
    #print "\n"
    logout(cookies)
    end=time.time()
    print (end-start)
if __name__ == '__main__':
    main()
